package com.sky.business.sys.controller;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.sky.business.sys.domain.TrackTask;
import com.sky.business.sys.param.TaskListParam;
import com.sky.business.sys.service.TrackTaskService;
import com.sky.common.api.Result;
import com.sky.common.utils.StringUtils;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.MediaType;
import org.springframework.web.bind.annotation.*;

import java.util.HashMap;
import java.util.Map;

@RestController
@RequestMapping("/trackTask")
@Slf4j
@Tag(name = "追踪任务管理", description = "追踪任务管理控制层")
public class TrackTaskController {

    @Autowired
    private TrackTaskService trackTaskService;

    @Operation(summary = "任务列表")
    @PostMapping(value = "/list", produces = MediaType.APPLICATION_JSON_VALUE)
    public Result<Map<String, Object>> list(@RequestBody TaskListParam param) {
        LambdaQueryWrapper<TrackTask> wrapper = new LambdaQueryWrapper<TrackTask>();
        if(StringUtils.isNotBlank(param.getContent())){
            wrapper.and(w -> w.likeLeft(TrackTask::getTaskId, param.getContent()).or().likeLeft(TrackTask::getDeviceId, param.getContent()));
        }
        if(param.getStatus() != null){
            wrapper.eq(TrackTask::getStatus, param.getStatus());
        }
        wrapper.orderByDesc(TrackTask::getCreateTime);
        IPage<TrackTask> iPage = trackTaskService.page(new Page<>(param.getPage(), param.getLimit()),wrapper);
        Map<String, Object> data = new HashMap<>();
        data.put("total", iPage.getTotal());
        data.put("records", iPage.getRecords());
        return Result.success(data);
    }
}
